<template >
    <el-container>
        <el-container>
         <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="200px" class="demo-ruleForm">
  <el-form-item label="用户Id" prop="applicantId">
    <el-input  v-model="ruleForm.applicantId" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="申请人姓名" prop="applicantName">
    <el-input  v-model="ruleForm.applicantName" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="购书申请ID" prop="applyId">
    <el-input v-model.number="ruleForm.applyId"></el-input>
  </el-form-item>
  <el-form-item label="书本ID" prop="bookId">
    <el-input v-model.number="ruleForm.bookId"></el-input>
  </el-form-item>
  <el-form-item label="库存" prop="stock">
    <el-input v-model.number="ruleForm.stock"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
      </el-form>
      </el-container>
    </el-container>
</template>

<script>
import userApi from '@/api/user'
  export default {
    data() {
      var checkStock = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('库存不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } 
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('不能为空'));
        }
        setTimeout(() => {
          if (!typeof(value)=='string') {
            callback(new Error('请输入字符串'));
          } 
        }, 1000);
      };
    
      return {
        ruleForm: {
          applicantId: '1401391094248062978',
          applicantName: 'ws',
          applyId: '',
          bookId:'1377816317712011300',
          stock:'11000'
        },
        rules: {
          applicantId: [
            { validator: validatePass, trigger: 'blur' }
          ],
          applicantName: [
            { validator: validatePass, trigger: 'blur' }
          ],
          applyId: [
            { validator: validatePass, trigger: 'blur' }
          ],
          bookId: [
            { validator: validatePass, trigger: 'blur' }
          ],
          stock:[
            { validator: checkStock, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
          
    async  submitForm(formName) {
       // this.$emit('submit')
       
      await userApi.submitApply(this.ruleForm).then(
        resp => {
           
         if(resp.data.success===true){ 
            this.$message({
            message: '恭喜你，成功了',
            type: 'success'
        })
        if(this.ruleForm.stock<10000) { this.$emit('toFinancial')}
          else  this.$emit('submit')
        }else{
            this.$message.error('错了哦，这是一条错误消息')
        }
      }
    )   
        

      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
.el-form-item{
    width: 500px;
}
</style>